package com.morningstar.flow.flowable.condition;

import cn.hutool.extra.spring.SpringUtil;
import com.morningstar.flow.antvX6.common.Condition;
import com.morningstar.flow.antvX6.common.ConditionGroup;
import com.morningstar.flow.antvX6.common.Group;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.delegate.DelegateExecution;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 条件处理器
 *
 * @author Admin
 */
@Slf4j
@Component
public class ConditionGroupHandler {

    /**
     * 执行条件判断处理器
     *
     * @param execution 当前执行实例
     * @return boolean
     */
    public boolean handle(DelegateExecution execution) {
        log.info("条件处理器{}", execution.getCurrentActivityId());
        return true;
    }

    /**
     * 处理条件组集合
     *
     * @param conditionGroup 条件组集合
     * @param formModel      表单值
     * @return boolean
     */
    public boolean handle(ConditionGroup conditionGroup, Map<String, Object> formModel) {
        boolean relationship = conditionGroup.isRelationship();
        List<Group> groupList = conditionGroup.getGroups();
        for (Group group : groupList) {
            boolean groupResult = this.handleGroup(group, formModel);
            // and 有一个失败就返回false
            if (relationship && !groupResult) {
                return false;
            }
            // or 有一个成功就返回true
            if (!relationship && groupResult) {
                return true;
            }
        }
        return relationship;
    }

    /**
     * 处理条件组
     *
     * @param group     条件组
     * @param formModel 表单值
     * @return boolean
     */
    public boolean handleGroup(Group group, Map<String, Object> formModel) {
        boolean relationship = group.isRelationship();
        List<Condition> conditions = group.getConditions();
        for (Condition condition : conditions) {
            boolean conditionResult = this.handleCondition(condition, formModel);
            // and 有一个失败就返回false
            if (relationship && !conditionResult) {
                return false;
            }
            // or 有一个成功就返回true
            if (!relationship && conditionResult) {
                return true;
            }
        }
        return relationship;
    }

    /**
     * 执行条件逻辑<br/>
     * SPEL 示例 <br/>
     * #formModel[表单key：input_100] 表达式： >=  值：5
     *
     * @param condition 条件
     * @param formModel 表单值
     * @return boolean
     */
    public Boolean handleCondition(Condition condition, Map<String, Object> formModel) {
        if (StringUtils.equalsIgnoreCase(condition.getType(), ConditionConstants.CONDITION_TYPE_SPEL)) {
            ExpressionParser parser = new SpelExpressionParser();
            EvaluationContext context = new StandardEvaluationContext();
            context.setVariable("formModel", formModel);
            Boolean result = parser.parseExpression(condition.getValue()).getValue(context, Boolean.class);
            log.info("执行条件逻辑{} {} {}", condition.getName(), condition.getValue(), result);
            return result;
        }
        if (StringUtils.equalsIgnoreCase(condition.getType(), ConditionConstants.CONDITION_TYPE_CLASS)) {
            ConditionHandler conditionHandler = SpringUtil.getBean(condition.getValue(), ConditionHandler.class);
            if (conditionHandler == null) {
                throw new RuntimeException("找不到对应类:" + condition.getValue());
            }
            Boolean result = conditionHandler.handle(formModel);
            log.info("执行条件逻辑{} {} {}", condition.getName(), condition.getValue(), result);
            return result;
        }
        throw new RuntimeException("条件类型配置错误");
    }
}
